package com.lly.bigdata.bgayx.controller;

import com.lly.bigdata.bgayx.base.BaseController;
import com.lly.bigdata.bgayx.service.ISysLoginService;
import com.lly.bigdata.bgayx.vo.AjaxResult;
import com.lly.bigdata.bgayx.vo.LoginBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/")
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    ISysLoginService sysLoginService;


    /**
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult postLogin(@RequestBody @Valid LoginBody loginBody, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            ObjectError objectError = allErrors.get(0);
            return AjaxResult.error(objectError.getDefaultMessage());
        }
        String token = sysLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        AjaxResult success = AjaxResult.success();
        success.put("data", token);
        return success;

    }
}
